package net.huashitong.controller.user;

import huashitong.util.StringUtils;
import net.huashitong.entity.UserMessage;
import net.huashitong.service.user.UserService;
import net.huashitong.shiro.UsernamePasswordToken;
import net.huashitong.ssydt.security.utils.Base64;
import net.huashitong.ssydt.security.utils.Result;
import net.huashitong.ssydt.security.utils.ResultUtils;
import net.huashitong.ssydt.user.entity.vo.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户模块
 *
 * @author defang.huang
 */

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户名登录
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<String> login(@RequestParam String mobile, @RequestParam String password, @RequestParam(required = false) String[] rememberMe) {
        boolean rememberme = false;
        if (rememberMe != null) {
            rememberme = true;
        }
        if (StringUtils.isBlank(mobile) && StringUtils.isBlank(password)) {
            return ResultUtils.getResults("500","账号密码不能为空");
        }

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(mobile, Base64.encode(password.getBytes()).toCharArray(),
                rememberme);
        try {
            SecurityUtils.getSubject().login(usernamePasswordToken);
        }catch (Exception e){
            return ResultUtils.getResults("500","账号或密码错误");
        }

        return ResultUtils.getResults("200", "登录成功");
    }

    /**
     * 发送验证码
     */
    @ResponseBody
    @RequestMapping(value = "/sms", method = RequestMethod.POST)
    public Result<String> sms(@RequestBody @Valid SmsVo smsVo) {
        userService.sms(smsVo, true);
        return ResultUtils.getSuccessResults();
    }

    /**
     * 修改密码
     */
    @PutMapping(value = "/my/password")
    public Result<String> passwordMy(@RequestBody @Valid PasswordMyVO passwordMyVO) {
        UserMessage userMessage = (UserMessage) SecurityUtils.getSubject().getPrincipal();
        userService.passwordMy(userMessage.getUserId(), passwordMyVO);
        return ResultUtils.getSuccessResults();
    }

    /**
     * 发送修改密码的信息
     */
    @PostMapping(value = "/my/sms")
    public Result<String> smsMy() {
        UserMessage userMessage = (UserMessage) SecurityUtils.getSubject().getPrincipal();
        userService.smsMy(userMessage.getUserId());
        return ResultUtils.getSuccessResults();
    }

    /**
     * 重置密码
     */
    @ResponseBody
    @RequestMapping(value = "/password", method = RequestMethod.POST)
    public Result<String> password(@RequestBody @Valid PasswordVO passwordVO) {
        userService.password(passwordVO);
        return ResultUtils.getSuccessResults();
    }

    /**
     * 注册
     */
    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result<String> register(@RequestBody @Valid RegisterVO registerVO) {
        userService.register(registerVO);
        return ResultUtils.getResults(Result.Status.SUCCESS, "注册成功");
    }

    /**
     * 验证手机号是否注册,验证手机号是否注册，若results返回ture代表已经注册
     */
    @ResponseBody
    @RequestMapping(value = "/mobile/{mobile}/isRegister")
    public Result<IsRegisterVO> mobileIsRegister(@PathVariable("mobile") String mobile) {
        IsRegisterVO vo = userService.mobileIsRegister(mobile);
        return ResultUtils.getSuccessResults(vo);
    }

    /**
     * 退出
     */
    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Result<String> logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout(); // session 会销毁，在SessionListener监听session销毁，清理权限缓存
        }

        return ResultUtils.getResults(Result.Status.SUCCESS, "退出成功");
    }
    /**
     * 返回用户协议
     */
    @RequestMapping(value = "/agreement")
    public String agreement(){
        return "user/agreement";
    }

}
